package top.toptimus.util;

import com.google.common.base.Strings;

import java.math.BigDecimal;

/**
 * 值对象工具类
 *
 * @author gaoyu
 */
public class ValueUtil {
    /**
     * 空值处理
     *
     * @param nullValue 空值
     * @return 处理过的数据
     */
    public static String StringNullValue(String nullValue) {
        return Strings.isNullOrEmpty(nullValue) ? "" : nullValue;
    }

    public static String BigDecimalNullValue(BigDecimal nullValue) {
        try {
            return Strings.isNullOrEmpty(nullValue.toString()) ? "" : nullValue.toString();
        } catch (Exception e) {
            return "0.0";
        }
    }

    public static int IntNullValue(String nullValue) {
        try {
            return Integer.parseInt(nullValue);
        } catch (Exception e) {
            return 0;
        }
    }

    public static Double DoubleNullValue(String nullValue) {
        try {
            return Double.parseDouble(nullValue);
        } catch (Exception e) {
            return 0.0;
        }
    }

    public static String objectToSting(Object o) {
        try {
            return o.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String generateNextCode(String barcode, String prefix) {
        return prefix + String.format("%0" + (barcode.length() - prefix.length()) + "d", Integer.parseInt(barcode.substring(prefix.length(), barcode.length())) + 1);
    }

    public static Double doubleNullValue(Double nullValue) {
        if (nullValue == null) {
            return 0.0;
        } else
            return nullValue;
    }
}
